using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using LambdaBuilder.Resolver;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.BasePC.UI.SystemIni;
using SqlSugarEx;
using SqlSugarEx.Utilities;

namespace Catastrophe.BasePC.UI.DbDal
{
	public class SysEncodeDal : ISysEncodeDal
	{
		protected SqlSugarClientEx _dbSugar;

		public SysEncodeDal()
		{
			this._dbSugar = DbUtility.GetSugar(Catastrophe.Initialize.SystemIni.FileConfigIni.GetDbAppConfig, isAutoCloseCon: true);
		}

		public DataTable QueryEncodeBind(string encodeName)
		{
			return this._dbSugar.QueryableBind<SysEncodeEntity>().WhereIF(!string.IsNullOrEmpty(encodeName), (SysEncodeEntity x) => x.EncodeName.Contains(encodeName)).ToDataTable();
		}

		public bool UpdateEncodeBind()
		{
			return this._dbSugar.UpdateBind();
		}

		public Tuple<bool, string> GetNewEncodeNo<T1>(string columnName, Expression<Func<T1, object>> exprEncodeName, Expression<Func<T1, object>> exprSort, bool isDbSame = true, string lastRowData = "") where T1 : class, new()
		{
			if (columnName == null)
			{
				throw new ArgumentNullException("columnName");
			}
			StringBuilder stringBuilder = new StringBuilder();
			string item = "";
			try
			{
				string name = "";
				string text = "";
				if (exprEncodeName != null)
				{
					name = LambdaResolver.GetColumnName(exprEncodeName);
				}
				if (exprSort != null)
				{
					text = LambdaResolver.GetColumnName(exprSort);
				}
				List<SysEncodeEntity> list = this._dbSugar.Queryable<SysEncodeEntity>().ToList();
				foreach (SysEncodeEntity item2 in list)
				{
					if (!(item2.EncodeName.Trim() == columnName.Trim()) || string.IsNullOrWhiteSpace(columnName) || !item2.IsAutoEncode)
					{
						continue;
					}
					if (!string.IsNullOrWhiteSpace(item2.HeadInfo) && !string.IsNullOrWhiteSpace(lastRowData) && !lastRowData.Contains(item2.HeadInfo))
					{
						lastRowData = "";
					}
					stringBuilder.Append(item2.HeadInfo);
					if (!string.IsNullOrWhiteSpace(item2.MiddleStyle))
					{
						stringBuilder.Append(DateTime.Now.ToString(item2.MiddleStyle));
					}
					if (!(item2.LastInStyle == "递增"))
					{
						continue;
					}
					string text2 = "";
					if (isDbSame)
					{
						T1 val = this._dbSugar.Queryable<T1>().OrderBy(text + " DESC").First();
						if (val != null)
						{
							PropertyInfo property = typeof(T1).GetProperty(name);
							if (property == null)
							{
								throw new Exception("编码字段不存在！");
							}
							text2 = property.GetValue(val)!.ToString();
						}
					}
					else
					{
						text2 = lastRowData;
					}
					if (text2 == "")
					{
						stringBuilder.Append('0', item2.LastBit - item2.StartIndex.ToString().Length);
						stringBuilder.Append(item2.StartIndex.ToString());
						continue;
					}
					string s = "0";
					string text3 = "";
					bool flag = false;
					if (!string.IsNullOrWhiteSpace(item2.MiddleStyle))
					{
						string value = "";
						text3 = DateTime.Now.ToString(item2.MiddleStyle);
						if (text2.Length >= item2.HeadInfo.Length + item2.MiddleStyle.Length)
						{
							value = text2.Substring(item2.HeadInfo.Length, item2.MiddleStyle.Length).Trim();
						}
						else
						{
							flag = true;
						}
						if (!text3.Equals(value))
						{
							flag = true;
						}
					}
					if (!flag && !string.IsNullOrWhiteSpace(text2))
					{
						s = text2.Remove(0, (item2.HeadInfo + text3).Length);
					}
					if (int.TryParse(s, out var result))
					{
						result += item2.AddCount;
						stringBuilder.Append('0', (item2.LastBit - result.ToString().Length > 0) ? (item2.LastBit - result.ToString().Length) : 0);
						stringBuilder.Append(result);
						continue;
					}
					return new Tuple<bool, string>(item1: false, item);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			item = stringBuilder.ToString().Trim();
			return new Tuple<bool, string>(item.Length > 0, item);
		}
	}
}
